package com.mou.controller.user;

import com.mou.constant.MessageConstant;
import com.mou.context.BaseContext;
import com.mou.dto.VerifyDTO;
import com.mou.entity.Verify;
import com.mou.result.Result;
import com.mou.service.VerifyService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;

@RestController("userVerifyController")
@RequestMapping("/user/verify")
@Slf4j
public class VerifyController {
    @Autowired
    private VerifyService verifyService;

    @PostMapping("/submit")
    public Result<String> verify(@RequestBody VerifyDTO verifyDTO) {
        log.info("用户申请认证：{}", verifyDTO);
        Integer currentUserId = BaseContext.getCurrentId();
        if(!Objects.equals(verifyDTO.getUserId(), currentUserId)){
            return Result.error(MessageConstant.ILLEGAL_OPERATION);
        }
        List<Verify> verifyList = verifyService.getByUserId(currentUserId);
        if(!verifyList.isEmpty()){
            return Result.error(MessageConstant.APPLY_EXIST);
        }
        Verify verify = new Verify();
        BeanUtils.copyProperties(verifyDTO, verify);
        verify.setUserId(currentUserId);
        verify.setCreateTime(LocalDateTime.now());
        verify.setIsPass(1);
        verifyService.save(verify);
        return Result.success();
    }
}
